/**
 * @param {string} url 接口地址
 * @param {string} method 请求方法：GET、POST，只能大写
 * @param {JSON} [params=''] body的请求参数，默认为空
 * @return 返回Promise
 */

function initForm(obj) {
	var formdata = new FormData();
	for(var o in obj) { //variable 为属性名
	  formdata.append(o, obj[o]);
	}
	return formdata;
}
let fetchRequest = function(url, method, params, header,asyncStatus) {
	let common_url = "http://192.168.10.181:8081/mer-api/api"; //服务器地址
	let default_header = new Headers();
	var header = header ? header : default_header;
	let finalUrl = (url.split("")[0].indexOf('/') > -1) ? (common_url + url) : url;
    if(params === '') { //如果网络请求中没有参数
		return new Promise(function(resolve, reject) {
			fetch(finalUrl, {
				   async:false,
					method: method,
					headers: header
				}).then((response) => response.json())
				.then((responseData) => {
					 
                       resolve(responseData);
					 
                    
				
				})
				.catch((err) => {
				  reject(err);
				});
		});
	} else { //如果网络请求中带有参数

		return new Promise(function(resolve, reject) {
			 
			fetch(finalUrl, {
					method: method,
					headers: header,
					body:initForm(params)
				}).then((response) => response.json())
				.then((responseData) => {
					 
                       resolve(responseData);
					 
				})
				.catch((err) => {
					 reject(err);
				});
		});
	}
}
export {
	fetchRequest
};